********************************************************************************
** Do file: g13_endline_moneyborrowed.do
** First started: June 22, 2022
** Last edited: December 31, 2023

/* Purpose: this do file analyzes several outcome variables related to money borrowed

Part I: Effect on Formal Money Borrowed, Informal Money Borrowed, and Total Money Borrowed in the Last 6 Months, Levels (and excluding loans that are not attributable to specific sources) - Winsorized, ITT

* ITEM MADE: TABLE A.15
*/

********************************************************************************

clear
clear matrix
clear mata
set maxvar 10000

** Preamble - data management for both the individual level dataset and household level dataset **

** Household **

* Load key data first
use "$merged/key_rep.dta", clear
keep if hhid!=.

* Restrict down to sample of interest
keep if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_surveyed==1 & k_surveyed_end==1

* Merge in variables from both baseline and endline
merge 1:1 hhid using "$bsvy_clean/hhvars_baseline.dta", keepusing(value_hh_assets total_loans)
keep if _merge==3 | _merge==1
drop _merge

* NOTE: Rename the baseline version of total loans
ren total_loans total_loans_bl

* Merge endline survey variables
merge 1:1 hhid using "$esvy_clean/3A_assets_hh.dta", keepusing(buy_assets_val99p sell_assets_val99p net_assets_val99p)
keep if _merge==3 | _merge==1
drop _merge

merge 1:1 hhid using "$esvy_clean/6_bsl.dta", keepusing(total_loans_r amt_formal_win amt_informal_win amt_forminform_win diff_fi_win)
keep if _merge==3 | _merge==1
drop _merge
		
* Drop the choice group as well
drop if treatmenttype_sh=="R T3"

* Develop indicators
g anytreat_assigned = (treatmenttype_sh=="R T1-L" | treatmenttype_sh=="R T1-U" | treatmenttype_sh=="R T2-U") 
g locked_assigned = (treatmenttype_sh=="R T1-L")
g surprise_assigned = (treatmenttype_sh=="R T1-U")
g unlocked_assigned = (treatmenttype_sh=="R T2-U")

/* We want to have this speak to a similar sample as in the assets group, although the sample
here will be slightly smaller gievn the variables involved */

* Make sure that the sample is the same throughout for assets 
g assets_sample_r = 1 if net_assets_val99p!=. 

* Construct varriables here
g assets_val_end = value_hh_assets + buy_assets_val99p - sell_assets_val99p
g total_loans_end_r = total_loans_bl + total_loans_r
g av_tl_end_r = assets_val_end - total_loans_end_r

g assets_sample2_r = 1 if assets_val_end!=. & total_loans_end_r!=. & av_tl_end_r!=. 

* Make the samples the same, they are close
g assets_sample_full = 1 if assets_sample_r==1 & assets_sample2_r==1

*****************************************************************************************************

* Further restrict based on a variable that features in the below analysis
replace assets_sample_full = . if amt_forminform_win==.



************
** Part I **
************

***** TABLE A.15 *****

*** Effect on Formal Money Borrowed, Informal Money Borrowed, and Money Borrowed in the Last 6 Months, Levels (ITT; Using the Winsorized Variables)

* Outcome means
sum amt_formal_win if anytreat_assigned!=. & assets_sample_full==1 & anytreat_assigned==0
sum amt_informal_win if anytreat_assigned!=. & assets_sample_full==1 & anytreat_assigned==0
sum amt_forminform_win if anytreat_assigned!=. & assets_sample_full==1 & anytreat_assigned==0
sum diff_fi_win if anytreat_assigned!=. & assets_sample_full==1 & anytreat_assigned==0

reg amt_formal_win locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1
test locked_assigned = unlocked_assigned

reg amt_informal_win locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1
test locked_assigned = unlocked_assigned

reg amt_forminform_win locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1
test locked_assigned = unlocked_assigned

reg diff_fi_win locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1
test locked_assigned = unlocked_assigned

* OLS results
eststo: reg amt_formal_win anytreat_assigned if assets_sample_full==1
eststo: reg amt_formal_win locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1

eststo: reg amt_informal_win anytreat_assigned if assets_sample_full==1
eststo: reg amt_informal_win locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1

eststo: reg amt_forminform_win anytreat_assigned if assets_sample_full==1
eststo: reg amt_forminform_win locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1

eststo: reg diff_fi_win anytreat_assigned if assets_sample_full==1
eststo: reg diff_fi_win locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1

esttab using "$tables/endline_moneyborrowed.tex", ///
	b(0) se(0) se replace booktabs star(* .10 ** .05 *** .01) title(Effect on Formal, Informal, and Total Money Borrowed in the Last 6 Months\label{endline_moneyborrowed}) ///
	label nodepvars mtitles("Formal money borrowed" "Formal money borrowed" "Informal money borrowed" "Informal money borrowed" "Money borrowed" "Money borrowed" "Formal minus informal" "Formal minus informal") nonotes ///
	varlabels(anytreat_assigned "Pooled"  locked_assigned "Secured" surprise_assigned "Surprise Unsecured" unlocked_assigned "Unsecured" _cons "Constant", ///
	elist(anytreat_assigned \addlinespace locked_assigned \addlinespace surprise_assigned \addlinespace unlocked_assigned \addlinespace)) 
eststo clear
